/*
Title: graphmfx
Authors: Nate Kelly
Institution: University of Tennessee
Version: Beta 1.3
Updates:Adds functionality xtmixed and xtmelogit
Date: 7/14/2014
Contact Nathan.J.Kelly@gmail.com with any bug reports/fixes.

List of additions that need to be made:

Write code to allow for mi datasets.
The command looks at range existing in dataset, need to change that to
in the model.
*/


program define graphmfx
version 8

#delimit ;
syntax anything(name=mainvar) [if], condition1(string) interaction1(string) [xtitle(string asis) ytitle(string asis) level(cilevel)];
;
#delimit cr

capture drop marg se upper lower

local command=e(cmd)
local dv=e(depvar)

if "`command'"=="regress" | "`command'"=="xtpcse" | "`command'"=="xtreg" {
local x `mainvar'
local xz `interaction1'
local z `condition1'
local cilevel=`level'/100
display "CI Level = `level'%, One Tailed"
local zvalue = invnorm(`cilevel')
display "Z-Score = `zvalue'"

matrix b=e(b) 
matrix V=e(V)
}

if "`command'"=="logit" | "`command'"=="probit" | "`command'"=="gllamm" | "`command'"=="xtmixed" | "`command'"=="xtmelogit" {
local x "`dv':`mainvar'"
local xz "`dv':`interaction1'"
local z `condition1'
local cilevel=`level'/100
display "CI Level = `level'%, One Tailed"
local zvalue = invnorm(`cilevel')
display "Z-Score = `zvalue'"

matrix b=e(b) 
matrix V=e(V)
} 

if "`command'"=="xtmelogit" {
local x "eq1:`mainvar'"
local xz "eq1:`interaction1'"
local z `condition1'
local cilevel=`level'/100
display "CI Level = `level'%, One Tailed"
local zvalue = invnorm(`cilevel')
display "Z-Score = `zvalue'"

matrix b=e(b) 
matrix V=e(V)
} 


matrix b1=b[1,"`x'"]
scalar b1=b1[1,1]
matrix b3=b[1,"`xz'"]
scalar b3=b3[1,1]
matrix varb1=V["`x'","`x'"]
scalar varb1=varb1[1,1] 
matrix varb3=V["`xz'","`xz'"]
scalar varb3=varb3[1,1]
matrix covb1b3=V["`x'","`xz'"]
scalar covb1b3=covb1b3[1,1]

local mainvarL `x'
local colpos=strpos("`x'",":")
local dotpos=strpos("`x'",".")
local length=length("`x'")
local mainvar1 = substr("`x'",`colpos'+1,`length')
local mainvar = substr("`mainvar1'",`dotpos'+1,`length')
macro drop colpos mainvar1 dotpos length
local condvarL ``z''
local colpos=strpos("`z'",":")
local dotpos=strpos("`z'",".")
local length=length("`z'")
local condvar1 = substr("`z'",`colpos'+1,`length')
local condvar = substr("`condvar1'",`dotpos'+1,`length')
macro drop colpos convar1 dotpos length

gen marg=scalar(b1)+(scalar(b3)*`condvar')
display "Calculated condition variable=`condvar'"
gen se=sqrt(scalar(varb1)+((`condvar'^2)*scalar(varb3))+(2*`condvar'*scalar(covb1b3)))
gen upper = marg+(se*`zvalue')
gen lower = marg-(se*`zvalue')

if missing("`xtitle'") {
local xtitle "Value of `condvar'"
}
if missing("`ytitle'") {
local ytitle "Effect of `mainvar' with `cilevel'% C.I."
}

#delimit ;
;
local graphcmd "twoway (line marg `condvar', sort clcolor(black)) (line upper `condvar', 
sort clpattern(dash) clcolor(black)) (line lower `condvar', sort clpattern(dash) 
clcolor(black)) `if', legend(off) yline(0) ytitle(`ytitle') xtitle(`xtitle')";
;
display "The graph command executed is as follows: `graphcmd'";
;
twoway (line marg `condvar', sort clcolor(black)) (line upper `condvar', 
sort clpattern(dash) clcolor(black)) (line lower `condvar', sort clpattern(dash) 
clcolor(black)) `if', legend(off) yline(0) ytitle("`ytitle'") xtitle("`xtitle'");
;
end;
